/**下午3:27:48
 * @author zhangyh2
 * MainActivity.java
 * TODO
 */
package cn.com.darly.videoandroidpm.ui.fragment;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.darly.common.NetUtils;
import com.darly.common.ToastApp;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseFragment;
import cn.com.darly.videoandroidpm.bean.FragmentMainInfo;
import cn.com.darly.videoandroidpm.common.SharePreferHelp;
import cn.com.darly.videoandroidpm.ui.adapter.FragmentMainRecyclerAdapter;
import cn.com.darly.videoandroidpm.ui.adapter.FragmentMainRecyclerAdapter.OnRecyclerItemClickListener;
import cn.com.darly.videoandroidpm.ui.biz.HomeFragmentListener.HomeFragmentInterface;
import cn.com.darly.videoandroidpm.ui.presenter.HomeFragmentPresenter;
import cn.com.darly.videoandroidpm.widget.waveswiperefresh.WaveSwipeRefreshLayout;
import cn.com.darly.videoandroidpm.widget.waveswiperefresh.WaveSwipeRefreshLayout.OnRefreshListener;

/**
 * @author zhangyh2 MainActivity 下午3:27:48 TODO
 */
public class HomeFragment extends BaseFragment implements OnRefreshListener,OnClickListener,OnRecyclerItemClickListener,HomeFragmentInterface {

	WaveSwipeRefreshLayout id_frgment_main_swipe;
	RecyclerView id_frgment_main_list;
	ImageView id_fragment_main_refresh;
	private FragmentMainRecyclerAdapter adapter;

	Map<String, String> params = new LinkedHashMap<String, String>();

	private int pageNo = 1;

	private int pageSize = 10;

	private ObjectAnimator rote;

	private HomeFragmentPresenter presenter;

	private OrnamentalFlowerFragment ornamental;

	private SucculentFragment succulent;

	private VenusaurFragment venusaur;


	@Override
	protected int root() {
		return R.layout.fragment_main;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseActivity#initView(android.os.Bundle)
	 */
	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		id_frgment_main_swipe = rootView.findViewById(R.id.id_frgment_main_swipe);
		id_frgment_main_list = rootView.findViewById(R.id.id_frgment_main_list);
		id_fragment_main_refresh =  rootView.findViewById(R.id.id_fragment_main_refresh);
		presenter = new HomeFragmentPresenter(this);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseActivity#loadData()
	 */
	@Override
	protected void loadData() {
		id_frgment_main_swipe.setColorSchemeColors(Color.WHITE, Color.WHITE);
		id_frgment_main_swipe.setWaveColor(Color.argb(100,255,0,0));

		adapter = new FragmentMainRecyclerAdapter(getActivity(), null);
		LinearLayoutManager manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
		if (id_frgment_main_list != null) {
			id_frgment_main_list.setLayoutManager(manager);
			id_frgment_main_list.setHasFixedSize(true);
			id_frgment_main_list.setAdapter(adapter);
		}
		if (NetUtils.isNetworkAvailable(getActivity())){
			params.put("pageNo", String .valueOf(pageNo));
			params.put("pageSize", String .valueOf(pageSize));
			presenter.indexAmuse(params);
		}else {
			if (SharePreferHelp.getValue("HomeFragment")!=null) {
				List<FragmentMainInfo> info = (List<FragmentMainInfo>) SharePreferHelp.getValue("HomeFragment");
				adapter.setData(info);
			}
		}

	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hellen.base.BaseActivity#initListener()
	 */
	@Override
	protected void initListener() {
		id_frgment_main_swipe.setOnRefreshListener(this);
		adapter.setOnRecyclerItemClickListener(this);
		id_fragment_main_refresh.setOnClickListener(this);
	}



	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.id_fragment_main_refresh:
				rote = ObjectAnimator.ofFloat(id_fragment_main_refresh, "rotation", 0, 360);
				rote.setRepeatCount(Integer.MAX_VALUE);
				rote.start();
				refresh();
				break;
		}
	}

	@Override
	public void onRefresh() {
		refresh();
	}

	private void refresh(){
		pageNo++;
		params.put("pageNo", String .valueOf(pageNo));
		params.put("pageSize", String .valueOf(pageSize));
		presenter.indexAmuse(params);
	}

	@Override
	public void OnRecyclerItemClick(RecyclerView parent, View view, int position, FragmentMainInfo data) {
		ToastApp.showToast(data.getContent());
	}

	@Override
	public void onSuccess(List<FragmentMainInfo> info) {
		adapter.setData(info);
		if (rote!=null)		rote.cancel();
		if (id_frgment_main_swipe!=null)	id_frgment_main_swipe.setRefreshing(false);
	}

	@Override
	public void onFailed(String info) {
		ToastApp.showToast(info);
		if (pageNo == 1){
			//第一页连接失败，使用缓存
			if (SharePreferHelp.getValue("HomeFragment")!=null) {
				List<FragmentMainInfo> infreo = (List<FragmentMainInfo>) SharePreferHelp.getValue("HomeFragment");
				adapter.setData(infreo);
			}
		}
		if (rote!=null)		rote.cancel();
		if (id_frgment_main_swipe!=null)	id_frgment_main_swipe.setRefreshing(false);
	}

	@Override
	public void setDisableClick() {

	}

	@Override
	public void setEnableClick() {

	}
}
